﻿using System;
using System.Collections.Generic;
using CosyTrade.Organization;
using CosyTrade.UI;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace CosyTrade.ClientForm
{
    public partial class SelectEmployee : UserControlBase
    {
        public SelectEmployee()
        {
            InitializeComponent();
            Load += delegate { InitData(); };
        }

        public event EventHandler OnSelected;
        private Employee curremp;
        private bool loadAllEmployee;

        /// <summary>
        /// �Ƿ�������й�˾��Ա��,Ĭ��ֵΪ false ���ص�ǰ��˾Ա��
        /// </summary>
        public bool LoadAllEmployee
        {
            get { return loadAllEmployee; }
            set { loadAllEmployee = value; }
        }

        public Employee GetSelectedEmployee
        {
            get { return curremp; }
        }

        private void InitData()
        {
            if (DesignMode) return;
            treeList1.ClearNodes();

            if (!loadAllEmployee)
            {
                IList<Employee> employees = Operation.OrganizationSystem.GetEmployeeList(GlobalInfo.CurrentCompany.PrimaryKey);
                if (employees == null) return;

                foreach (Employee employee in employees)
                {
                    TreeListNode listNode = treeList1.AppendNode(new object[] { employee.EmpName, employee }, null);
                    listNode.HasChildren = false;
                    listNode.Tag = false;
                }
            }
            else
            {
                IList<Company> companies = Operation.OrganizationSystem.GetCompanyList();
                if (companies == null) return;

                foreach (Company company in companies)
                {
                    TreeListNode node = treeList1.AppendNode(new object[] {company.Caption, company}, null);
                    node.HasChildren = true;
                    node.Tag = false;
                }
            }
        }

        private void treeList1_BeforeExpand(object sender, BeforeExpandEventArgs e)
        {
            if (e.Node.Tag == null) return;
            if ((bool)e.Node.Tag) return;
            e.Node.Tag = true;

            Company company = (Company)e.Node.GetValue("ME");
            if (company == null) return;
            IList<Employee> employees = Operation.OrganizationSystem.GetEmployeeList(company.PrimaryKey);
            if (employees == null) return;

            foreach (Employee employee in employees)
            {
                TreeListNode listNode = treeList1.AppendNode(new object[] {employee.EmpName, employee}, e.Node);
                listNode.HasChildren = false;
                listNode.Tag = false;
            }
        }

        //private void treeList1_DoubleClick(object sender, EventArgs e)
        //{
        //    object businessObject = treeList1.Selection[0].GetValue("ME");
        //    if (businessObject == null) return;
        //    if (!businessObject.GetType().Name.Equals("Employee")) return;
        //    curremp = businessObject as Employee;
            
        //    if (OnSelected != null) OnSelected(this, new EventArgs());
        //}

        private void treeList1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left || e.Clicks < 2) return;

            TreeListHitInfo hitInfo = treeList1.CalcHitInfo(e.Location);
            if (hitInfo.Node == null) return;

            curremp = hitInfo.Node.GetValue("ME") as Employee;

            if (curremp != null && OnSelected != null) OnSelected(this, new EventArgs());
        }
    }
}
